package com.yjyw.tiexie.util;

import android.app.Activity;

import com.yjyw.tiexie.bean.WXParams;
import com.tencent.mm.opensdk.constants.Build;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;

public class PayUtils {
    /**
     * 微信支付
     *
     * @param wxParams
     * @param api
     */
    public static void doWechatPay(Activity activity, WXParams wxParams, IWXAPI api) {

        try {
            if (!api.isWXAppInstalled()) {
                ToastUtil.show("请先安装微信");
                return;
            }
            //检查微信版本是否支持支付
            boolean isPaySupported = api.getWXAppSupportAPI() >= Build.PAY_SUPPORTED_SDK_INT;
            if (isPaySupported) {
                PayReq req = new PayReq();
                //公众账号ID
                req.appId = wxParams.getAppid();
                //商户号
                req.partnerId = wxParams.getPartnerid();
                //预支付交易会话ID
                req.prepayId = wxParams.getPrepayid();
                //随机字符串
                req.nonceStr = wxParams.getNoncestr();
                //时间戳
                req.timeStamp = wxParams.getTimestamp();
                //扩展字段
                req.packageValue = wxParams.getPackageX();
                //签名
                req.sign = wxParams.getSign();
                // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
                api.sendReq(req);
            } else {
                ToastUtil.show("您当前微信不支持支付功能");
            }

        } catch (Exception e) {
            e.printStackTrace();

        }
    }
}
